/*
 * @Date: 2023-04-03 18:59:36
 * @LastEditTime: 2023-09-14 18:23:27

 * 介绍:时间工具
 */
import { fixedNumber } from "./format";
import dayjs, { Dayjs } from "dayjs";
interface getCountdownRes {
  /**相差总毫秒，不大于1则到期 */
  diffTime: number;
  /**相差总毫秒 */
  MS: number;
  /**相差毫秒 */
  ms: string;
  /**相差总小时 */
  H: number;
  /**相差小时 */
  h: string;
  /**相差分钟 */
  /**相差总分钟 */
  M: number;
  m: string;
  /**相差总秒 */
  S: number;
  /**相差秒 */
  s: string;
  /**相差总天 */
  D: number;
}
/**
 * * 获取倒计时
 * @param day 当前时间
 * @param timeLimit 期限
 */
export function getCountdown(
  day: string | Date | Dayjs,
  timeLimit: string | Date | Dayjs
): getCountdownRes {
  day = dayjs(day);
  timeLimit = dayjs(timeLimit);
  const res: getCountdownRes = {
    diffTime: 0,
    MS: 0,
    H: 0,
    h: "00",
    M: 0,
    m: "00",
    S: 0,
    s: "00",
    ms: "00",
    D: 0,
  };
  /**相差时间 */
  res.diffTime = timeLimit.diff(day, "ms");
  if (res.diffTime <= 0) return res;
  res.MS = res.diffTime;
  res.D = Number(timeLimit.diff(day, "day"));
  /**相差毫秒 */
  res.ms = fixedNumber(Math.floor((res.MS / 10) % 100));
  /**相差秒 */
  res.S = timeLimit.diff(day, "second");
  res.s = fixedNumber(res.S % 60);
  /**相差分 */
  res.M = timeLimit.diff(day, "minute");
  res.m = fixedNumber(res.M % 60);
  /**相差总小时 */
  res.H = timeLimit.diff(day, "hour");
  res.h = fixedNumber(res.H % 24);
  return res;
}
